<template>
  <div class="loginWrap">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="账号">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toLogin">立即登录</el-button>
      </el-form-item>
      <el-form-item label="其他登录方式">
        <Button>
          <a
            href="https://gitee.com/oauth/authorize?client_id=7d2351470a92b220703e37d4f1e047227ea2b21990bad36c1c4bd946b4f338d5&redirect_uri=http://localhost:8080/login&response_type=code"
            >GITEE</a
          >
        </Button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { throttle } from "../utils/throttle";
import { postUrl } from "../utils/request";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },
  async created() {
    let code = this.$route.query;
    if (code) {
      const res = await postUrl("/oauth", code);
      console.log(res);
      if (res.data.code === 200) {
        alert(res.data.mes);
        this.$router.push("/home");
      } else {
        alert(res.data.mes);
      }
    }
  },
  methods: {
    async toLogin() {
      throttle(this.onSubmit, 2000);
    },
    async onSubmit() {
      const res = await postUrl("/login", this.form);
      console.log(res);
      if (res.data.code === 200) {
        localStorage.setItem("token", res.data.token);
        alert(res.data.mes);
        this.$router.push('/home')
      } else {
        alert(res.data.mes);
      }
    },
  },
};
</script>

<style lang='scss'>
.loginWrap {
  width: 500px;
  height: 300px;
  background: #fff;
  padding: 20px;
  border-radius: 15px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>